//
//  FlutterTextDrawImpl.swift
//  Runner
//
//  Created by 莫华军 on 2024/9/21.
//

import Foundation
import Flutter

//文字绘制
class FlutterTextDrawImpl {
    static let sharedInstance = FlutterTextDrawImpl()
    private var methodChannel: FlutterMethodChannel?
    private  init() {
        
    }
    
    func setFlutterBinaryMessenger(binaryMessenger:FlutterBinaryMessenger){
        //文字相关的本地方法
        methodChannel = FlutterMethodChannel(name: "lib_draw_text", binaryMessenger: binaryMessenger)
        methodChannel?.setMethodCallHandler { (call:FlutterMethodCall, result:@escaping FlutterResult) in
            
            if (call.method == "drawText") {
                
                if let args = call.arguments as? [String: Any]{
                    let text = args["text"] as? String
                    let width = args["width"] as? Int
                    let height = args["height"] as? Int
                    var textHeight = height
                    if(args.keys.contains("textHeight")){
                        textHeight = args["textHeight"] as? Int
                    }
                    
                    let arr = TextDataTool.getTextData(withStr: text!, size: CGSize.init(width: width!, height: height!),textHeight: CGFloat(textHeight!))
                    
                    let dict: [String: Any] = [
                        "data": arr,
                    ]
                    result(dict)
                }
            }else if(call.method == "setBold"){
                TextDataTool.setBold(call.arguments as! Bool)
                result(["":""])
            }else if(call.method == "setItalic"){
                TextDataTool.setItalic(call.arguments as! Bool)
                result(["":""])
            }else if(call.method == "setFontFilePath"){
                var path = call.arguments as! String
                if(path.isEmpty){
                    print("字体路径为空 :\(path)")
                    TextDataTool.setFontName("")
                }else{
                    print("字体路径 :\(path)")
                    self.registerFontFromFile(path)
                }
                result(["":""])
            }
        }
    }


    func registerFontFromFile(_ fontPath: String) {
        guard let fontData = try? Data(contentsOf: URL(fileURLWithPath: fontPath)) else {
            print("Error loading font data")
            return
        }

        guard let fontProvider = CGDataProvider(data: fontData as CFData) else {
            print("Error creating font provider")
            return
        }

        guard let fontRef = CGFont(fontProvider) else {
            print("Error creating font reference")
            return
        }
        // 获取 PostScript 名称
        if let postScriptName = fontRef.postScriptName {
            print("PostScript Name: \(postScriptName)")
            TextDataTool.setFontName(postScriptName as String)
        }

        var error: Unmanaged<CFError>?
        if CTFontManagerRegisterGraphicsFont(fontRef, &error) {
            print("Font registered successfully: \(fontRef)")
        } else {
            print("Error registering font: \(error.debugDescription)")
        }
    }

}
